#!/bin/sh
VERSION="2.1.5"

if ! git checkout release-vl ; then
	echo 'cannot checkout release-vl'
	exit 1
fi

if ! git merge dev -m "merge dev into release-vl" ; then
	echo 'cannot merge dev into release-vl'
	exit 1
fi

if ! git tag -a "v$VERSION" -m "自编译发行版" ; then
	if ! git tag -d "v$VERSION" ; then
		echo 'cannot delete tag release-vl'
		exit 1
	fi
	if ! git push origin ":refs/tags/v$VERSION" ; then
		echo 'cannot delete tag release-vl'
		exit 1
	fi
	if ! git tag -a "v$VERSION" -m "自编译发行版" ; then
		echo 'cannot tag release-vl'
		exit 1
	fi
fi

if ! git push ; then
	echo 'cannot push release-vl'
	exit 1
fi

if ! git checkout dev-automake ; then
	echo 'cannot checkout dev-automake'
	exit 1
fi

if ! git merge dev --no-ff -m "merge dev_${VERSION} into dev-automake" ; then
	echo 'cannot merge dev into dev-automake'
	exit 1
fi

if ! git push ; then
	echo 'cannot push dev-automake'
	exit 1
fi


if ! git checkout release-automake ; then
	echo 'cannot checkout release-automake'
	exit 1
fi

if ! git merge dev-automake --no-ff -m "merge dev-automake_${VERSION} into release-automake" ; then
	echo 'cannot merge dev-automake into release-automake'
	exit 1
fi

if ! git tag -a "v$VERSION-automake" -m "automake 发行版" ; then
	if ! git tag -d "v$VERSION-automake" ; then
		echo 'cannot delete tag release-automake'
		exit 1
	fi
	if ! git push origin ":refs/tags/v$VERSION-automake" ; then
		echo 'cannot delete tag release-automake'
		exit 1
	fi
	if ! git tag -a "v$VERSION-automake" -m "automake 发行版" ; then
		echo 'cannot tag release-automake'
		exit 1
	fi
fi

if ! git push ; then
	echo 'cannot push release-automake'
	exit 1
fi


if ! git checkout release-automake-under2.7 ; then
	echo 'cannot checkout release-automake-under2.7'
	exit 1
fi

if ! git merge dev-automake --no-ff -m "merge dev-automake_${VERSION} into release-automake-under2.7" ; then
	echo 'cannot merge dev-automake into release-automake-under2.7'
	exit 1
fi


if ! git tag -a "v$VERSION-automake-under2.7" -m "automake 发行版, 用于 automake 版本低于 2.7" ; then
	if ! git tag -d "v$VERSION-automake-under2.7" ; then
		echo 'cannot delete tag release-automake-under2.7'
		exit 1
	fi
	if ! git push origin ":refs/tags/v$VERSION-automake-under2.7" ; then
		echo 'cannot delete tag release-automake-under2.7'
		exit 1
	fi

	if ! git tag -a "v$VERSION-automake-under2.7" -m "automake 发行版, 用于 automake 版本低于 2.7" ; then
		echo 'cannot tag release-automake-under2.7'
		exit 1
	fi
fi


if ! git push ; then
	echo 'cannot push release-automake-under2.7'
	exit 1
fi

if ! git checkout dev ; then
	echo 'cannot checkout dev'
	exit 1
fi

if ! git push origin --tags ; then
	echo 'cannot push tags'
	exit 1
fi

echo 'success'